1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.hash;
18
19 import com.google.caliper.BeforeExperiment;
20 import com.google.caliper.Benchmark;
21 import com.google.caliper.Param;
22
23 import java.security.MessageDigest;
24
25
26
27
28
29
30 public class MessageDigestCreationBenchmark {
31
32 @Param({"MD5", "SHA-1", "SHA-256", "SHA-512"})
33 private String algorithm;
34
35 private MessageDigest md;
36
37 @BeforeExperiment void setUp() throws Exception {
38 md = MessageDigest.getInstance(algorithm);
39 }
40
41 @Benchmark int getInstance(int reps) throws Exception {
42 int retValue = 0;
43 for (int i = 0; i < reps; i++) {
44 retValue ^= MessageDigest.getInstance(algorithm).getDigestLength();
45 }
46 return retValue;
47 }
48
49 @Benchmark int clone(int reps) throws Exception {
50 int retValue = 0;
51 for (int i = 0; i < reps; i++) {
52 retValue ^= ((MessageDigest) md.clone()).getDigestLength();
53 }
54 return retValue;
55 }
56 }